package com.simple.piger.controller;


import com.alibaba.fastjson.JSONObject;
import com.simple.piger.component.CommonComponent;
import com.simple.piger.dto.req.InviteRecordReqDto;
import com.simple.piger.dto.resp.InviteRecordRespDto;
import com.simple.piger.entity.InviteRecord;
import com.simple.piger.enums.InviteRecordStatusEnums;
import com.simple.piger.service.InviteRecordService;
import com.simple.piger.utils.AjaxResult;
import com.simple.piger.utils.PublicUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 求职记录 前端控制器
 * </p>
 *
 * @since 2020-02-06
 */
@RestController
@RequestMapping("/inviteRecord")
public class InviteRecordController extends BaseController {
    @Autowired
    private InviteRecordService inviteRecordService;
    @Autowired
    private CommonComponent commonComponent;

    /**
     * 申请求职接口
     * @param hashMap
     * @return
     */
    @PostMapping("/invite")
    public AjaxResult invite(@RequestParam HashMap hashMap) {
        JSONObject jsonObject = PublicUtil.getJSONObject(hashMap);
        InviteRecordReqDto reqDto = JSONObject.toJavaObject(jsonObject, InviteRecordReqDto.class);
        reqDto.setCmsSysUserId(getValueFromSession("userId"));
        reqDto.setCompanyId(commonComponent.getCompanyIdByCmsUserId(reqDto.getCmsSysUserId()));
        return inviteRecordService.invite(reqDto);
    }

    /**
     * 插叙求职列表的接口
     * @param hashMap
     * @return
     */
    @GetMapping("/list")
    public AjaxResult applyUserList(@RequestParam HashMap hashMap) {
        JSONObject jsonObject = PublicUtil.getJSONObject(hashMap);
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");

        String cmsSysUserId = getValueFromSession("userId");
        List<InviteRecordRespDto> list = inviteRecordService.list(page, limit, cmsSysUserId);
        int count = inviteRecordService.count(cmsSysUserId);
        return success(count, list);
    }

    /**
     * 公司 查看求职记录的接口
     * @param hashMap
     * @return
     */
    @GetMapping("/company/list")
    public AjaxResult companyList(@RequestParam HashMap hashMap) {
        JSONObject jsonObject = PublicUtil.getJSONObject(hashMap);
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");

        String cmsSysUserId = getValueFromSession("userId");
        String companyId = commonComponent.getCompanyIdByCmsUserId(getValueFromSession("userId"));
        List<InviteRecordRespDto> list = inviteRecordService.companyList(page, limit, cmsSysUserId, companyId);
        int count = inviteRecordService.companyListCount(cmsSysUserId, companyId);
        return success(count, list);
    }

    @DeleteMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") String id) {
        InviteRecord record = inviteRecordService.selectById(id);
        record.setStatus(InviteRecordStatusEnums.$1.getItem());
        record.setUpdateTime(new Date());
        record.updateById();
        return success();
    }

    @PutMapping("/firstPass/{id}")
    public AjaxResult firstPass(@PathVariable("id") String id) {
        InviteRecord record = inviteRecordService.selectById(id);
        record.setStatus(InviteRecordStatusEnums.$2.getItem());
        record.setUpdateTime(new Date());
        record.updateById();
        return success();
    }

    @PutMapping("/review/{id}")
    public AjaxResult review(@PathVariable("id") String id) {
        InviteRecord record = inviteRecordService.selectById(id);
        record.setStatus(InviteRecordStatusEnums.$3.getItem());
        record.setUpdateTime(new Date());
        record.updateById();
        return success();
    }
}
